CKEDITOR.plugins.add('remoteImage', {
    requires: 'filetools',
    icons: 'remoteImage',
    beforeInit: function (editor) {
        if (!!!CKEDITOR.fileTools) {
            console.log("Please add the plugins fileTools and its requirements.")
        }
    },
    init: function (editor) {
        editor.ui.addButton('remoteImage', {
            label: '远程存图',
            command: 'remoteToLocal',
            toolbar: 'insert'
        });

        editor.addCommand('remoteToLocal', {
            exec: function (editor) {
                var url = editor.config.remoteToLocalUrl;
                var oldContent = editor.getData();
                if(oldContent!=''){
                    $.ajax({
                        type: 'post',
                        cache: true,
                        async: true,
                        url:url,
                        data:{content:oldContent},
                        beforeSend: function () {
                            helper.loading('show');
                        },
                        error: function () {
                            layer.alert('与服务器断开连接！');
                            return false;
                        },
                        complete: function () {
                            helper.loading('hide');
                        },
                        success:function (res){
                            if(res.code==1){
                                editor.setData(res.data);
                            }else{
                                layer.alert(res.msg);
                            }
                        }
                    });
                }
            }
        });
    }
});